﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class Download : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            this.TextBox1.Text = Request.QueryString["a"];
            if (Request.QueryString["a"] != null) 
            {
                if (Request.QueryString["a"].ToUpper() != "NULL")
                {
                    string archivo = ConfigurationManager.AppSettings["Path_AgregarDocumento"] + Request.QueryString["a"];
                    // string archivo = Request.QueryString["a"];
                    FileInfo infoDoc = new FileInfo(archivo);
                    //new FileInfo(Environment.GetEnvironmentVariable("TEMP") + "\\" + Request.QueryString["a"]);
                    Response.Clear();
                    Response.ClearContent();
                    Response.ClearHeaders();
                    Response.ContentType = "application/octet-stream";
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + infoDoc.Name);
                    Response.AddHeader("Content-Length", infoDoc.Length.ToString());
                    Response.WriteFile(infoDoc.FullName);
                    this.TextBox1.Text = this.TextBox1.Text + ", " + infoDoc.FullName;
                    Response.Flush();
                    infoDoc.Delete();
                    infoDoc = null;
                    Response.End();
                }
            }
        }
        catch (Exception ex) { TextBox1.Text = TextBox1.Text + ", " + ex; }
    }
}
